"""
Case Type   : 资源池化
Case Name   : 资源池化场景下, 查看cm处于pause是否可以仲裁失效
Create At   : 2024-04-07
Owner       : songshixuan
Description :
    1、查看集群状态 cm_ctl query -Cv
    2、执行cm_ctl pause暂停cm集群
    3、构建备机kill -9 故障
    4、执行cm_ctl resume恢复cm集群
    5、查询集群状态
    6、恢复集群原始状态
Expect      :
    1、集群状态正常，数据库节点状态正常
    2、暂停集群成功
    3、由于暂停，备机不会被拉起
    4、恢复集群后将备节点拉起，拉起正常
    5、集群状态正常
    6、恢复集群原始状态成功,集群状态正常
History     :
    1、2024-04-07 创建
    2、2025-07-10 改造用例为1p1s环境可执行
"""

import os
import time
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')

@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()

    def test_shared_storage(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:cm_ctl pause; expect:cm_ctl 暂停-----'
        self.log.info(text)
        status = Primary_SH.exec_gs_ssh('rm -rf $GAUSSHOME/bin/cluster_manual_starting')
        self.log.info(status)
        status = Primary_SH.exec_cm_ctl(mode='pause')
        self.log.info(status)
        self.assertIn('has been paused', status, '执行失败' + text)

        text = '-----step3:构建主机kill -9故障; expect:主机不会重新被拉起，cm不会仲裁升主，备机unknow-----'
        self.log.info(text)
        queryCmd = Primary_SH.exec_cmd_under_user("ps ux | grep -v grep | grep 'bin/gaussdb'")
        pid = queryCmd.split()[1]
        stopdn = Primary_SH.exec_cmd_under_user("kill -9 " + pid)
        self.log.info(stopdn)
        # 睡眠一会
        time.sleep(20)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        res1 = status.splitlines()[-1].split('|')[0].split('6001')[-1]
        res2 = status.splitlines()[-1].split('|')[1].split('6002')[-1]
        self.log.info(res1)
        self.log.info(res2)
        self.assertIn('P Down    Starting', res1, '执行失败' + text)
        self.assertIn('S Standby Normal', res2, '执行失败' + text)

        text = '-----step4:cm_ctl resume; expect:执行成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='resume')
        self.log.info(status)
        self.assertIn('has been resumed', status, '执行失败' + text)

        text = '-----step5:停止30s秒后重新cm_ctl query查询; expect:集群状态正常-----'
        self.log.info(text)
        time.sleep(30)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)

    def tearDown(self):
        text = '-----step6:恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        status = Primary_SH.exec_cm_ctl(mode='resume')
        self.log.info(status)
        self.assertIn('has been resumed', status, '执行失败' + text)
        
        status = Primary_SH.exec_cm_ctl(mode='stop', param='')
        self.log.info(status)
        self.assertIn('successfully.', status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='start', param='')
        self.log.info(status)
        self.assertIn('successfully.', status, '执行失败' + text)
        recovery = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(recovery)
        self.assertIn(self.constant.cm_switchover_success_msg, recovery,
                      '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('P Primary Normal',
                      status.splitlines()[-1].split('|')[0].split('6001')[-1],
                      '执行失败' + text)
        self.assertIn('S Standby Normal',
                      status.splitlines()[-1].split('|')[1].split('6002')[-1],
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')

